<!-- 编辑区域 -->
<template>
  <div>
    <a-modal
      :title="$t('Edit-area')"
      v-model="modalVisible"
      width='30%'
      @ok="handleSubmit"
      @cancel="handleCancel"
      :maskClosable="false"
      v-dragModal
    >
      <a-form :form="form">
        <a-form-item :label="$t('Region-name')" v-bind="formLayout" has-feedback>
          <a-input v-decorator="['areaName',{
              rules:[
                {required: true, message:$t('Enter')+$t('Region-name')},
                {pattern:namePattern,message:$t('Not_enter_special_characters')}
              ],initialValue: targetArea.zone_name
          }]"
          >
          </a-input>
        </a-form-item>
        <a-form-item :label="$t('Is-read-only')" v-bind="formLayout">
          <a-switch
            v-decorator="['isReadonly',{ valuePropName: 'checked', initialValue: targetArea.readonly===1 ? true : false }]"
            :checked-children="$t('Yes')"
            :un-checked-children="$t('No')"
          >
          </a-switch>
        </a-form-item>
        <!-- <a-form-item :label="$t('Is-primary')" v-bind="formLayout">
          <a-switch
            v-decorator="['isMain',{ valuePropName: 'checked', initialValue: false }]"
            :checked-children="$t('Yes')"
            :un-checked-children="$t('No')"
          >
          </a-switch>
        </a-form-item> -->
      </a-form>
      <template slot="footer">
        <a-button @click="handleReset" class="submit-btn">
          {{$t('Reset')}}
        </a-button>
        <a-button
          class="submit-btn"
          type="primary"
          @click="handleSubmit"
          :loading="submitLoading"
        >
          {{$t('Sure')}}
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
export default {
  name: "EditArea",
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    targetArea: {}
  },

  data() {
    return {
      modalVisible: false,
      form: this.$form.createForm(this),
      formLayout:{
        labelCol: {span: 6},
        wrapperCol: {span: 17}
      },
      submitLoading: false,
    }
  },

  watch: {
    visible(newValue) {
      this.modalVisible = newValue;
    }
  },

  methods: {
    //提交编辑区域
    handleSubmit() {
      this.form.validateFields(async (err,value)=>{
        if(!err){
          this.submitLoading = true;
          const params = {
            zone_id: this.targetArea.zone_id,
            name: value.areaName,
            readonly: value.isReadonly ? 1 : 0
          }
          const res = await this.$api.areaManage.editAreaRequest(params);
          if(res && res.data) {
            this.model(res.data.id) //返回模板
            this.notice(res.data.id, res.data.params, res.data.flag) //通知列表显示
            if(res.data.flag === 0) {
              this.$emit('updateArea');
            }
          }
          this.handleCancel();
          this.submitLoading = false;
        }
      })
    },

    //重置表单
    handleReset() {
      this.form.resetFields(); //重置弹窗
    },

    //关闭弹窗
    handleCancel() {
      this.$emit('closeModal', 'edit-area');
      this.handleReset();
    },
  },
}
</script>
<style scoped>
  .submit-btn {
    width: 70px;
  }
</style>